package com.sg.service.base.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.app.req.ImplementRollbackOpeRecordLoopStartReq;
import com.sg.dto.base.app.req.QueryRollbackLockOperationRecordDetailReq;
import com.sg.dto.base.app.req.QueryRollbackLockOperationRecordListReq;
import com.sg.entity.RollbackLockOperationRecord;
import com.sg.service.base.app.MRollbackLockOperationRecordService;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.dal.mapper.mbg.RollbackLockOperationRecordMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MRollbackLockOperationRecordServiceImpl
        extends ServiceImpl<RollbackLockOperationRecordMapper, RollbackLockOperationRecord>
        implements MRollbackLockOperationRecordService {

    @Autowired
    RollbackLockOperationRecordMapper rollbackLockOperationRecordMapper;

    /**
     * 3-3-20新增回退锁定操作记录[5779]
     * gen by moon at 1/24/2023, 9:01:17 PM
     */
    @Trace(operationName = "3-3-20新增回退锁定操作记录")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addRollbackLockOperationRecord(RollbackLockOperationRecord rollbackLockOperationRecord) {
        rollbackLockOperationRecord.setLockOperationRecordId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        rollbackLockOperationRecord.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        rollbackLockOperationRecord.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        rollbackLockOperationRecord.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        rollbackLockOperationRecord.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        rollbackLockOperationRecord.setIsValid("FALSE");
        rollbackLockOperationRecord.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        rollbackLockOperationRecord.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(rollbackLockOperationRecord)) {
            return rollbackLockOperationRecord.getLockOperationRecordId();
        } else {
            return "-1";
        }

    }

    /**
     * 3-3-20查询回退锁定操作记录列表[5783]
     * gen by moon at 1/27/2023, 7:56:29 PM
     */
    @Trace(operationName = "3-3-20查询回退锁定操作记录列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<RollbackLockOperationRecord> queryRollbackLockOperationRecordList(QueryRollbackLockOperationRecordListReq reqDto) {

        try {
            QueryWrapper<RollbackLockOperationRecord> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(RollbackLockOperationRecord.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(RollbackLockOperationRecord.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(RollbackLockOperationRecord.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<RollbackLockOperationRecord>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-3-20删除回退锁定操作记录[5784]
     * gen by moon at 1/27/2023, 7:56:29 PM
     */
    @Trace(operationName = "3-3-20删除回退锁定操作记录")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteRollbackLockOperationRecord(String lockOperationRecordId) {

        return super.removeById(lockOperationRecordId);
    }

    /**
     * 3-3-20查询回退锁定操作记录详情[5786]
     * gen by moon at 4/2/2023, 6:55:39 PM
     */
    @Trace(operationName = "3-3-20查询回退锁定操作记录详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RollbackLockOperationRecord queryRollbackLockOperationRecordDetail(QueryRollbackLockOperationRecordDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            QueryWrapper<RollbackLockOperationRecord> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<RollbackLockOperationRecord> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                RollbackLockOperationRecord retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * M3锁定操作记录【循环开始】[7219]
     * gen by moon at 6/17/2023, 7:51:15 PM
     */
    @Trace(operationName = "M3锁定操作记录【循环开始】")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RollbackLockOperationRecord implementRollbackOpeRecordLoopStart(ImplementRollbackOpeRecordLoopStartReq reqDto) {

        try {

            QueryWrapper<RollbackLockOperationRecord> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<RollbackLockOperationRecord> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                RollbackLockOperationRecord retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

}

